<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-08-15 15:22:07
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;

use App\Http\Services\Service;
use App\Repositories\MpcProYarnRepository;

use Illuminate\Support\Facades\DB;

class ProYarnService extends Service
    {

    public function __construct(MpcProYarnRepository $Repository)
        {
        $this->repository = $Repository;
        }
    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($yarn)
            {
            if ($yarn->status_id != "0") {
                fail("出库失败！单据已出库！");
                }
            $yarn->storage->filter(function ($storage)
                {
                $storage->subStorage();
                });
            $yarn->status_id = 1;
            $yarn->save();
            });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($yarn)
            {
            if ($yarn->status_id != "1") {
                fail("取消出库失败！单据未出库！");
                }
            if ($yarn->pro_plan->process_id != 0 && $yarn->pro_plan->process_id != $yarn->pro_plan->process_route->details()->first()->process_id) {
                fail("该缸已刷卡，不能取消出库！");
                }
            $yarn->storage->filter(function ($storage)
                {
                $storage->nosubStorage();
                });
            $yarn->status_id = 0;
            $yarn->save();
            });
        }
    protected function took()
        {
        $code     = request('code');
        $auditor_ = request('auditor_');
        if (isEmpty($code)) {
            fail("条码不能为空！");
            }
        if (isEmpty($auditor_)) {
            fail("领纱人不能为空！");
            }
        return $this->repository->whereCode($code)->get()->filter(function ($v) use ($auditor_)
            {
            $v->took_id   = $auditor_;
            $v->tooked_at = date("Y-m-d H:i:s");
            $v->save();
            });

        }
    protected function notook()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($yarn)
            {
            $yarn->took_id   = 0;
            $yarn->tooked_at = null;
            });
        }
    }